package com.bailiban.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bailiban.pojo.Order;
import com.bailiban.pojo.User;
import com.bailiban.service.OrderService;
import com.bailiban.service.UserService;
import com.bailiban.service.impl.OrderServiceImpl;
import com.bailiban.service.impl.UserServiceImpl;
import com.bailiban.utils.OrderIdUtil;
import com.bailiban.utils.ResultMsg;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/order")
public class OrderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private OrderService orderService = new OrderServiceImpl();
    private UserService uService = new UserServiceImpl();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String method = request.getParameter("method");
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        if ("findAll".equals(method)) {
            List<Order> list = orderService.findAllOrder();
            String json = mapper.writeValueAsString(list);
            response.getWriter().write(json);
        } else if ("add".equals(method)) {
            // 获取购票人信息
            String name = request.getParameter("name");
            String idCard = request.getParameter("idCard");
            // 先添加用户信息
            User user = new User();
            user.setIdCard(idCard);
            user.setName(name);
            ResultMsg resultMsg = uService.addUser(user);

            // 购票，获取所购的票的id
            String ids = request.getParameter("tids");
            String[] idss = ids.split(",");
            ArrayList<Integer> tids = new ArrayList<Integer>();
            for (String string : idss) {
                int id = Integer.parseInt(string);
                tids.add(id);
            }
            // 生成订单号
            String oid = OrderIdUtil.getOrderIdByTime();
            Order order = new Order();
            order.setOid(oid);
            order.setOrderTime(new Date());
            // 查询用户id
            User user2 = uService.findUserByidCard(idCard);

            order.setUid(user2.getId());
            boolean flag = resultMsg.isFlag();
            if (flag) {
                for (Integer tid : tids) {
                    order.setTid(tid);
                    ResultMsg msg1 = orderService.addOrder(order);
                    flag = flag && msg1.isFlag();
                }
            }
            String json = mapper.writeValueAsString(resultMsg);
            response.getWriter().write(json);
        } else if ("delete".equals(method)) {
            String oid = request.getParameter("oid");
            ResultMsg resultMsg = orderService.deleteOrderByOid(oid);
            String json = mapper.writeValueAsString(resultMsg);
            response.getWriter().write(json);
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
